﻿#include "OrderTest.h"
#include <QTimer>
#include "FrameData.h"
#include "FoldOperator.h"
#include "DataStream.h"

OrderTest::OrderTest(QWidget *parent)
    : QDialog(parent)
	, m_ctpModel(m_waitSemaphore)
	, order(m_ctpModel)
{
    ui.setupUi(this);
	connect(ui.pbt_buyorder, SIGNAL(released()), this, SLOT(onBuyOrder()));
	QTimer::singleShot(10, this, SLOT(onInit()));
	connect(this, SIGNAL(sigPrice(float)), this, SLOT(onRecvPrice(float)));
	connect(ui.ledt_symbol, SIGNAL(editingFinished()), this, SLOT(setMonitorSymbol()));
	//m_nTimerID = this->startTimer(200);
}

//void OrderTest::timerEvent(QTimerEvent *event)
//{
//	if (event->timerId() == m_nTimerID) {
//		DataStream::FrameData dataStream;
//		FoldOperator foldOper;
//		std::list<std::string> pFiles;
//		foldOper.ListFiles(dataStream.GetDir(), pFiles);
//		std::list<std::string> pFrameFiles;
//		for (std::list<std::string>::iterator itfile = pFiles.begin(); itfile != pFiles.end(); ++itfile)
//		{
//			std::list<FData::FrameKItem> frameList;
//			dataStream.Get(frameList, *itfile);
//			if (frameList.empty())
//			{
//				continue;
//			}
//			pFrameFiles.emplace_back(*itfile);
//			for (std::list<FData::FrameKItem>::iterator itFK = frameList.begin(); itFK != frameList.end(); ++itFK)
//			{
//				if (ui.ledt_symbol->text().compare(itFK->SymbolName) != 0)
//				{
//					continue;
//				}
//				MData::PriceItem priceItem;
//				priceItem.UpdateTime = itFK->UpdateFrame.UpdateTime;
//				priceItem.UpdateMillisec = itFK->UpdateFrame.UpdateMillisec;
//				priceItem.LastPrice = itFK->KItem.Close > 0 ? itFK->KItem.Close : itFK->KItem.Open;
//				ui.ledt_lastPrice->setText(QString::number(priceItem.LastPrice));
//			}
//		}
//
//		if (!pFrameFiles.empty())
//		{
//			foldOper.RemoveFiles(dataStream.GetDir(), pFrameFiles);
//		}
//	}
//}

void OrderTest::onBuyOrder()
{
	//order.InsertOrder(ui.spin_volumn->value());
	int pVolumn = ui.spin_volumn->value();
	order.InsertOrder(pVolumn, ui.spin_tickCount->value());
}

void OrderTest::onInit()
{
	m_waitSemaphore.SetLockDuration(10);
	m_ctpModel.Init();
	m_waitSemaphore.SetLockDuration(5);
	m_ctpModel.QueryAccount();
	m_ctpModel.QueryOrders();
	m_ctpModel.QueryPositions("");
	m_ctpModel.QueryTrades();

	std::thread pthread([this]() {
		DataStream::FrameData dataStream;
		for (;;)
		{
			std::list<FData::FrameKItem> frameList;
			dataStream.Get(frameList);

			for (std::list<FData::FrameKItem>::iterator itFK = frameList.begin(); itFK != frameList.end(); ++itFK)
			{
				if (ui.ledt_symbol->text().compare(itFK->SymbolName) != 0)
				{
					continue;
				}
				MData::PriceItem priceItem;
				priceItem.UpdateTime = itFK->UpdateFrame.UpdateTime;
				priceItem.UpdateMillisec = itFK->UpdateFrame.UpdateMillisec;
				priceItem.LastPrice = itFK->KItem.Close > 0 ? itFK->KItem.Close : itFK->KItem.Open;
				ui.ledt_lastPrice->setText(QString::number(priceItem.LastPrice));
			}

			if (frameList.empty())
			{
				std::this_thread::sleep_for(std::chrono::milliseconds(500));
			}
		}

	});
	pthread.detach();
}

void OrderTest::onRecvPrice(float lastPrice)
{
	ui.ledt_lastPrice->setText(QString::number(lastPrice));
}

void OrderTest::setMonitorSymbol()
{
	order.SetMoniteSymbol(ui.ledt_symbol->text().toLocal8Bit().data());
}
